﻿//Mirco Töpfer

using System;
using System.Windows.Forms;
using pheidippides.app.Properties;
using pheidippides.configuration.Classes;
using pheidippides.exceptionmanager.Classes;
using pheidippides.logging.Classes;

namespace pheidippides.app.Forms
{
    public partial class ConfigForm : Form
    {
        #region Constructor

        public ConfigForm()
        {
            InitializeComponent();

            textBoxDBPath.Text =
                ConfigManager.GetInstance().GetConfigEnry(configuration.Interfaces.ConfigEntryName.DatabasePath);

            Changed = false;
        }

        #endregion

        #region Private Members

        private Boolean Changed;

        #endregion

        #region Private Methods

        private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            FormManager.FormManager.GetInstance().DisposeForm(this);
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog Dialog = new OpenFileDialog();

                DialogResult Result = Dialog.ShowDialog();

                if (Result.Equals(DialogResult.OK) && Dialog.FileName != null)
                {
                    textBoxDBPath.Text = Dialog.FileName;
                }
            }
            catch (Exception ex)
            {
                Logger.GetInstance().LogException("Fehler beim Auswählen der Datenbank", ex, LogLevel.Warning);
                ExceptionManager.GetInstance().RegisterException("Fehler beim auswählen der Datenbank", ex);
            }
        }

        private void textBoxDBPath_TextChanged(object sender, EventArgs e)
        {
            Changed = true;
        }

        private void buttonSpeichern_Click(object sender, EventArgs e)
        {
            ConfigManager.GetInstance().SetConfigEntry(configuration.Interfaces.ConfigEntryName.DatabasePath,
                                                       textBoxDBPath.Text);
        }

        private void buttonAbbrechen_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Changed)
            {
                DialogResult Result = MessageBox.Show(Resources.ConfigForm_ConfigForm_FormClosing_Möchten_Sie_die_Änderungen_Speichern_, Resources.ConfigForm_ConfigForm_FormClosing_Frage,
                                                      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                switch (Result)
                {
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                    case DialogResult.Yes:
                        buttonSpeichern_Click(this, null);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}